2 // DigestCalculationTest.m
5 // Created by Joey Deng on 1/28/20.
8 #import <XCTest/XCTest.h>
9 #import "dnssec_v2_crypto.h"
10 #include "mDNSEmbeddedAPI.h"
11 #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
13 @interface DigestCalculationTest : XCTestCase
17 @implementation DigestCalculationTest
20 char * const inputs[] = {
23 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
24 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
25 "one million repetitions of 'a'"
28 unsigned char outputs[][SHA1_OUTPUT_SIZE] = {
29 {0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d},
30 {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09},
31 {0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1},
32 {0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19, 0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59},
33 {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f}
35 unsigned char actual_output[SHA1_OUTPUT_SIZE];
37 XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA1_OUTPUT_SIZE);
39 for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
41 const unsigned char * const expected_output = outputs[i];
46 input = malloc(1000001);
47 memset(input, 'a', 1000000);
48 input[1000000] = '\0';
53 uint8_t calculated = calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_1,
54 actual_output, sizeof(actual_output));
55 XCTAssertTrue(calculated);
57 XCTAssertTrue(memcmp(actual_output, expected_output, SHA1_OUTPUT_SIZE) == 0);
62 char * const inputs[] = {
65 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
66 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
67 "one million repetitions of 'a'"
70 unsigned char outputs[][SHA256_OUTPUT_SIZE] = {
71 {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad},
72 {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
73 {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1},
74 {0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1},
75 {0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0}
77 unsigned char actual_output[SHA256_OUTPUT_SIZE];
79 XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA256_OUTPUT_SIZE);
81 for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
83 const unsigned char * const expected_output = outputs[i];
88 input = malloc(1000001);
89 memset(input, 'a', 1000000);
90 input[1000000] = '\0';
95 uint8_t caculated = calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_256,
96 actual_output, sizeof(actual_output));
97 XCTAssertTrue(caculated);
99 XCTAssertTrue(memcmp(actual_output, expected_output, SHA256_OUTPUT_SIZE) == 0);
104 char * const inputs[] = {
107 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
108 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
109 "one million repetitions of 'a'"
112 unsigned char outputs[][SHA384_OUTPUT_SIZE] = {
113 {0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58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x2a, 0x5b, 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb, 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85}
119 unsigned char actual_output[SHA384_OUTPUT_SIZE];
121 XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA384_OUTPUT_SIZE);
123 for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
125 const unsigned char * const expected_output = outputs[i];
130 input = malloc(1000001);
131 memset(input, 'a', 1000000);
132 input[1000000] = '\0';
137 uint8_t calculated = calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_384,
138 actual_output, sizeof(actual_output));
139 XCTAssertTrue(calculated);
141 XCTAssertTrue(memcmp(actual_output, expected_output, SHA384_OUTPUT_SIZE) == 0);
146 #endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)